<?php
namespace app\admin\controller;
use app\admin\common\Base;
use think\Db;
use think\facade\Request;
use think\Exception;
use app\admin\model\Cdr as CdrModel;

/**
* 通话记录控制器
*/
class Cdr extends Base{

    use \app\admin\traits\BaseFn;

    protected $model = null;

    public function initialize(){
        parent::initialize();
        $this->model = new CdrModel();
    }



    //展示客户电话号码最近的5个【接通】的通话记录
    public function clientCdr(){
        if(Request::isAjax()){
            $order_phone = $this->request->post('order_phone', '', 'trim');


            $rows = $this->model
                ->where('src|dst', '=',$order_phone)    //高级查询
                ->where("callresult='ANSWERED' AND answered_tag='Y'")   //【接听了的通话的判断条件】
                ->where("lastapp!='Queue'")
                ->order('starttime desc')
                ->limit(5)
                ->select();

            foreach($rows as &$item){
                $item['billsec_val'] = sprintf("%02d", intval($item['billsec']/3600)). ":".
                    sprintf("%02d", intval(($item['billsec']%3600)/60)). ":".
                    sprintf("%02d", $item['billsec']%3600%60);
                $item['calltype_val'] = ["IN"=>"呼入", "OUT"=>"呼出", "INNER"=>"内部"][ $item['calltype'] ];
            }
            unset($item);
            $this->assign('rows', $rows->toArray($rows));

            return $this->fetch();
        }

    }

    public function record(){
        $this->assign([
            'uniqueid' => $this->request->get('uniqueid', '', 'trim'),
            'accountcode' => $this->request->get('accountcode', '', 'trim'),
            'inner_exten' => $this->request->get('inner_exten', '', 'trim'),
        ]);
        return $this->fetch();
    }

    //展示订单通话记录(20个)
    public function orderCdr(){
        $order_phone = $this->request->get('order_phone', '', 'trim');

        $rows = $this->model
            ->where('src|dst', '=',$order_phone)    //高级查询
            ->where("callresult='ANSWERED' AND answered_tag='Y'")
            ->where("lastapp!='Queue'")
            ->order('starttime desc')
            ->limit(5)
            ->select();

        foreach($rows as &$item){
            $item['billsec_val'] = sprintf("%02d", intval($item['billsec']/3600)). ":".
                sprintf("%02d", intval(($item['billsec']%3600)/60)). ":".
                sprintf("%02d", $item['billsec']%3600%60);
            $item['calltype_val'] = ["IN"=>"呼入", "OUT"=>"呼出", "INNER"=>"内部"][ $item['calltype'] ];
        }
        unset($item);
        $this->assign('rows', $rows->toArray($rows));

        return $this->fetch();
    }

}
